﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.Enum;


namespace AssetManageSystem.ViewModel.AssetManage.AssetTypeVMs
{
    public partial class AssetTypeListVM : BasePagedListVM<AssetType_View, AssetTypeSearcher>
    {
        public List<TreeSelectListItem> AllParents { get; set; }
        protected override void InitVM()
        {
            AllParents = DC.Set<AssetType>().GetTreeSelectListItems(Wtm, x => x.TypeName);
        }

        protected override List<GridAction> InitGridAction()
        {
            return new List<GridAction>
            {
                this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-green"),
                this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-blue"),
                this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-8"),
                // this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-orange"),
                this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                // this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-4"),
                // this.MakeStandardAction("AssetType", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], "AssetManage").SetButtonClass("btn-col-4"),
            };
        }


        protected override IEnumerable<IGridColumn<AssetType_View>> InitGridHeader()
        {
            return new List<GridColumn<AssetType_View>>{
                this.MakeGridHeader(x => x.TypeName),
                this.MakeGridHeader(x => x.TypeCode),
                this.MakeGridHeader(x => x.DisplayOrder),
                this.MakeGridHeader(x => x.Status),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<AssetType_View> GetSearchQuery()
        {
            var query = DC.Set<AssetType>()
                .Where(x =>x.Status != CommonStatus.Disable)
                .CheckContain(Searcher.TypeName, x => x.TypeName)
                .CheckEqual(Searcher.ParentId, x => x.ParentId).ToList();
            var topdata = query.Where(x => x.ParentId == null).ToList().FlatTree(x => x.DisplayOrder).ToList();
            topdata.ForEach((x) => { int l = x.GetLevel(); for (int i = 0; i < l; i++) { x.TypeName = "&nbsp;&nbsp;&nbsp;&nbsp;" + x.TypeName; } });
            int order = 0;
            var data = topdata.Select(x => new AssetType_View
            {
                ID = x.ID,
                TypeName = x.TypeName,
                TypeCode = x.TypeCode,
                DisplayOrder = x.DisplayOrder,
                Status = x.Status,
                Children = x.Children?.Select(y => new AssetType_View
                {
                    ID = y.ID,
                }),
                ExtraOrder = order++,
                ParentID = x.ParentId,
                Parent = x.Parent,
                HasChild = (x.Children != null && x.Children.Count() > 0) ? true : false,
            })
            .OrderBy(x => x.ExtraOrder);
            return data.AsQueryable() as IOrderedQueryable<AssetType_View>;
        }

    }

    public class AssetType_View : AssetType
    {
        [Display(Name = "父类型名称")]
        public String TypeName_view { get; set; }
        public bool HasChild { get; set; }
        public new IEnumerable<AssetType_View> Children { get; set; }
        public new AssetType Parent { get; set; }
        public Guid? ParentID { get; set; }
        public int ExtraOrder { get; set; }


    }
}
